# Standard libs
import base64
import hashlib
import logging
import urllib

# App Engine imports
from google.appengine.ext import webapp

# Third party imports
import json

# OpenSocial Gifts imports
from db import Games

logging.getLogger().setLevel(logging.INFO)

class ApiServer(webapp.RequestHandler):
  """Handles requests to /gifts URLs and reponds with JSON strings."""

  def _returnGames(self):
    """Return the list of gift names and keys as a JSON string"""
    games = []
    for game in Games.all():
      item = {'key' : str(game.key()),
              'name' : game.name,
              'src' : game.src }
      games.append(item)
      self.response.out.write('<a href="/games?id=%s">%s</a>'% (str(game.key()),game.name))
    
  def _getGame(self, gameid):
      results = []
      results = Games.get(gameid)
      self.response.out.write(results.src)

  def get(self):
    """Respond with a JSON string representation of the lists of gifts."""
    id = self.request.get('id')
    if id != '':
    	self._getGame(id)
    else:
    	self._returnGames()
